package com.wn.class30.configuration;



import com.wn.class30.entity.AdminUser;
import com.wn.class30.entity.Systemmenu;
import com.wn.class30.service.AdminUserService;
import com.wn.class30.service.SystemmenuService;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import javax.annotation.Resource;
import java.util.HashSet;

/** 
* @author 周豪 E-mail: 
* @version 创建时间：2020年3月7日 下午2:18:20 
* 类说明 :shiro realm
*/

public class UserRealm extends AuthorizingRealm {
/*@Resource
private MeunService meunService;
@Resource
private UserService userService;*/
	@Resource
	private AdminUserService adminUserService;
	@Resource
	private SystemmenuService systemmenuService;
//权限校验
	@Override
	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
		// TODO Auto-generated method stub
		AdminUser user=(AdminUser) principals.getPrimaryPrincipal();
		SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
		try {
			HashSet<Systemmenu> pression = systemmenuService.queryPressionByUserInfo(user.getAdminUserId());
			for (Systemmenu menu : pression) {
				simpleAuthorizationInfo.addStringPermission(menu.getMenuUrl());
			}
		} catch (Exception e) {

			e.printStackTrace();
		}


		return simpleAuthorizationInfo;
	}


	//认证
	@Override
	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
		// TODO Auto-generated method stub
		try {
			System.out.println(token.getPrincipal());
			AdminUser query = adminUserService.selectAdminUserByTel((String) token.getPrincipal());
			System.out.println(query);
		if(query!=null) {
			SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(query, query.getAdminUserPwd(),getName());
			return simpleAuthenticationInfo;
		}
		}catch(Exception e) {
			throw new AuthenticationException("账号错误");
		}
		return null;
	}
}
